package com.dfd.common.exception;

import com.dfd.common.po.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * 统一异常处理类
 *
 */
@ControllerAdvice
@Slf4j
public class CommonExceptionAdvice {
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseVo> handleException(Exception e) {
        log.error("全局异常拦截器捕获到异常：", e);
        ResponseVo responseVo;
        if (e instanceof CustomException) {
            // 自定义异常
            CustomException exception = (CustomException) e;
            responseVo = new ResponseVo(exception.getCode(), exception.getMsg());
        } else {
            // 自定义异常
            responseVo = new ResponseVo(100, e.getMessage());
        }
        return ResponseEntity.ok(responseVo);
    }
}
